﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;

namespace proyecto
{
    /// <summary>
    /// Lógica de interacción para heatMap.xaml
    /// </summary>
    public partial class heatMap : UserControl
    {


        int nMicroarrays;

        DockPanel [] panels;

        Label [][] labels;
    

        public heatMap()
        {
            InitializeComponent();
        }

        public heatMap(DataTable corr)
        {

            InitializeComponent();

            nMicroarrays = corr.Columns.Count - 1;

            panels = new DockPanel[nMicroarrays + 1];

            labels = new Label[nMicroarrays + 1][];

            

            for (int i = 0; i < nMicroarrays; i++)
            {
                panels[i] = new DockPanel();
                //panels[i].FlowDirection = FlowDirection.LeftToRight;
                panels[i].Height = Height/(nMicroarrays+1);
                panels[i].Width = Width;
                DockPanel.SetDock(panels[i], Dock.Top);
                labels[i] = new Label[nMicroarrays+1];

                for (int j = 0; j < nMicroarrays + 1; j++)
                {
                    labels[i][j] = new Label();
                    if (j != 0)
                    {
                        labels[i][j].Background = new SolidColorBrush(Color.FromArgb(255, Convert.ToByte(255 * (1.0-Convert.ToDouble(corr.Rows[i][j]))), Convert.ToByte(255 * (1.0-Convert.ToDouble(corr.Rows[i][j]))), Convert.ToByte(255 * (1.0-Convert.ToDouble(corr.Rows[i][j])))));
                        labels[i][j].Content = corr.Rows[i][j].ToString();
                        labels[i][j].Foreground = Brushes.Yellow;
                        labels[i][j].HorizontalContentAlignment = HorizontalAlignment.Center;
                        labels[i][j].VerticalContentAlignment = VerticalAlignment.Center;
                    }

                    else
                    {
                        labels[i][j].Content = corr.Rows[i][j].ToString();
                        
                    }

                    labels[i][j].Height = Height / (nMicroarrays + 1);
                    labels[i][j].Width = Width / (nMicroarrays + 1);
                    DockPanel.SetDock(labels[i][j], Dock.Left);

                    panels[i].Children.Add(labels[i][j]);
                }

                ListBoxItem item1 = new ListBoxItem();
                item1.Content = panels[i];
                dockPanel1.Items.Add(item1);
                
                //dockPanel1.Children.Add(panels[i]);
            }

            panels[nMicroarrays] = new DockPanel();
            panels[nMicroarrays].FlowDirection = FlowDirection.LeftToRight;
            panels[nMicroarrays].Height = Height/(nMicroarrays+1);
            DockPanel.SetDock(panels[nMicroarrays], Dock.Top);
            labels[nMicroarrays] = new Label[nMicroarrays+1];
            for (int j = 0; j < nMicroarrays + 1; j++)
            {
                labels[nMicroarrays][j] = new Label();
                if (j != 0)
                {
                    labels[nMicroarrays][j].Content = corr.Columns[j].Caption;
                }

                labels[nMicroarrays][j].Height = Height / (nMicroarrays + 1);
                labels[nMicroarrays][j].Width = Width / (nMicroarrays + 1);
                DockPanel.SetDock(labels[nMicroarrays][j], Dock.Left);

                panels[nMicroarrays].Children.Add(labels[nMicroarrays][j]);
            }

            ListBoxItem item2 = new ListBoxItem();
            item2.Content = panels[nMicroarrays];
            dockPanel1.Items.Add(item2);

            //dockPanel1.Children.Add(panels[nMicroarrays]);    


            this.SizeChanged += new SizeChangedEventHandler(heatMap_SizeChanged);
        }

        public heatMap(DataTable table, bool any)
        {


            InitializeComponent();

            nMicroarrays = table.Columns.Count - 1;

            //dockPanel1.LastChildFill = false;

            panels = new DockPanel[table.Rows.Count+1];

            labels = new Label[table.Rows.Count+1][];

            panels[0] = new DockPanel();
            panels[0].FlowDirection = FlowDirection.LeftToRight;

            panels[0].Height = 28;

            //panels[0].Height = Height / (table.Rows.Count + 1);
            DockPanel.SetDock(panels[0], Dock.Top);
            labels[0] = new Label[nMicroarrays + 1];
            for (int j = 0; j < nMicroarrays + 1; j++)
            {
                labels[0][j] = new Label();
                
                
                labels[0][j].Content = table.Columns[j].Caption;

                labels[0][j].Height = 28;

                //labels[0][j].Height = Height / (table.Rows.Count + 1);
                labels[0][j].Width = Width / (nMicroarrays + 1);
                DockPanel.SetDock(labels[0][j], Dock.Left);

                panels[0].Children.Add(labels[0][j]);
            }

            ListBoxItem item2 = new ListBoxItem();
            item2.Content = panels[0];
            dockPanel1.Items.Add(item2);

            //dockPanel1.Children.Add(panels[0]);

            for (int i = 1; i < table.Rows.Count; i++)
            {
                panels[i] = new DockPanel();
                //panels[i].FlowDirection = FlowDirection.LeftToRight;
                //panels[i].Height = Height / (table.Rows.Count + 1);
                panels[i].Height = 28;
                panels[i].Width = Width;
                DockPanel.SetDock(panels[i], Dock.Top);
                labels[i] = new Label[nMicroarrays + 1];

                for (int j = 0; j < nMicroarrays + 1; j++)
                {
                    labels[i][j] = new Label();
                    if (j != 0)
                    {
                        labels[i][j].Background = new SolidColorBrush(Color.FromArgb(255, Convert.ToByte(255 * (1.0 - Convert.ToDouble(table.Rows[i-1][j]))), Convert.ToByte(255 * (1.0 - Convert.ToDouble(table.Rows[i-1][j]))), Convert.ToByte(255 * (1.0 - Convert.ToDouble(table.Rows[i-1][j])))));
                        labels[i][j].ToolTip = table.Rows[i-1][j].ToString();
                        labels[i][j].Foreground = Brushes.Yellow;
                        labels[i][j].HorizontalContentAlignment = HorizontalAlignment.Center;
                        labels[i][j].VerticalContentAlignment = VerticalAlignment.Center;
                        
                    }

                    else
                    {
                        labels[i][j].Content = table.Rows[i-1][j].ToString();

                    }
                    labels[i][j].Height = 28;
                    //labels[i][j].Height = Height / (table.Rows.Count + 1);
                    labels[i][j].Width = Width / (nMicroarrays + 1);
                    DockPanel.SetDock(labels[i][j], Dock.Left);

                    panels[i].Children.Add(labels[i][j]);
                }

                ListBoxItem item1 = new ListBoxItem();
                item1.Content = panels[i];
                dockPanel1.Items.Add(item1);

                //dockPanel1.Children.Add(panels[i]);
            }

            ;

            



        }

        /*public heatMap(DataTable corr, double min, double max)
        {

            InitializeComponent();

            nMicroarrays = corr.Columns.Count - 1;

            panels = new DockPanel[corr.Columns.Count];

            labels = new Label[corr.Columns.Count][];



            for (int i = 0; i < nMicroarrays; i++)
            {
                panels[i] = new DockPanel();
                //panels[i].FlowDirection = FlowDirection.LeftToRight;
                panels[i].Height = Height / (nMicroarrays + 1);
                panels[i].Width = Width;
                DockPanel.SetDock(panels[i], Dock.Top);
                labels[i] = new Label[nMicroarrays + 1];

                for (int j = 0; j < nMicroarrays + 1; j++)
                {
                    labels[i][j] = new Label();
                    if (j != 0)
                    {
                        labels[i][j].Background = new SolidColorBrush(Color.FromArgb(255, Convert.ToByte(255 * ((Convert.ToDouble(corr.Rows[i][j]) - min) / (max - min))), Convert.ToByte(255 * ((Convert.ToDouble(corr.Rows[i][j]) - min) / (max - min))), Convert.ToByte(255 * ((Convert.ToDouble(corr.Rows[i][j])-min)/(max-min)))));
                        labels[i][j].Content = corr.Rows[i][j].ToString();
                        labels[i][j].Foreground = Brushes.Yellow;
                        labels[i][j].HorizontalContentAlignment = HorizontalAlignment.Center;
                        labels[i][j].VerticalContentAlignment = VerticalAlignment.Center;
                    }

                    else
                    {
                        labels[i][j].Content = corr.Rows[i][j].ToString();

                    }

                    labels[i][j].Height = Height / (nMicroarrays + 1);
                    labels[i][j].Width = Width / (nMicroarrays + 1);
                    DockPanel.SetDock(labels[i][j], Dock.Left);

                    panels[i].Children.Add(labels[i][j]);
                }

                dockPanel1.Children.Add(panels[i]);
            }

            panels[corr.Columns.Count] = new DockPanel();
            panels[corr.Columns.Count].FlowDirection = FlowDirection.LeftToRight;
            panels[corr.Columns.Count].Height = Height / (corr.Columns.Count + 1);
            DockPanel.SetDock(panels[corr.Columns.Count], Dock.Top);
            labels[corr.Columns.Count] = new Label[nMicroarrays + 1];
            for (int j = 0; j < nMicroarrays + 1; j++)
            {
                labels[corr.Columns.Count][j] = new Label();
                if (j != 0)
                {
                    labels[corr.Columns.Count][j].Content = corr.Columns[j].Caption;
                }

                labels[corr.Columns.Count][j].Height = Height / (nMicroarrays + 1);
                labels[corr.Columns.Count][j].Width = Width / (nMicroarrays + 1);
                DockPanel.SetDock(labels[corr.Columns.Count][j], Dock.Left);

                panel[corr.Columns.Count].Children.Add(labels[corr.Columns.Count][j]);
            }

            dockPanel1.Children.Add(panels[corr.Columns.Count]);


            this.SizeChanged += new SizeChangedEventHandler(heatMap_SizeChanged);
        }*/

        private void heatMap_SizeChanged(object sender, SizeChangedEventArgs e)
        {

            for (int i = 0; i < nMicroarrays + 1; i++)
            {
                panels[i].Height = Height / (nMicroarrays + 1);
                for (int j = 0; j < nMicroarrays + 1; j++)
                {
                    labels[i][j].Height = Height / (nMicroarrays + 1);
                    labels[i][j].Width = Width / (nMicroarrays + 1);
                }
            }
        }
            
            
                





    }
}
